
import axios from 'axios'
import { store } from '@/store/index.js'

const baseURL = '/gallerynew/h5/'

axios.defaults.timeout = 30000

export default (option) => {
  let opt = Object.assign({
    method: 'get'
  }, option)

  if (opt.url[0] !== '/') {
    opt.url = baseURL + opt.url
  }

  return new Promise((resolve, reject) => {
    axios(opt).then(response => {
      const data = response.data
      const code = data.code
      if (code === 0) {
        resolve(data)
      } else {
        reject(data)
        if (code === -100) {
          store.commit('common/setParams', {loginDisplay: true})
        }
      }
    }).catch(e => {
      let msg = ''
      if (e && e.code && e.code === 'ECONNABORTED') {
        msg = '网络连接超时，请检查网络'
      } else {
        msg = e.toString()
      }

      let obj = {
        code: -50,
        msg: msg
      }

      reject(obj)
    })
  })
}
